package com.sample.transaction.seata.sys.controller;

import com.sample.transaction.seata.BusinessApplication;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/business")
@RequiredArgsConstructor
public class BusinessController {

    private final BusinessApplication.OrderService orderService;

    private final BusinessApplication.StorageService storageService;

    // http://localhost:4003/business/insertSuccess
    @GlobalTransactional(timeoutMills = 300000, name = "spring-cloud-demo-tx")
    @GetMapping("insertSuccess")
    public Boolean insertSuccess(){
        orderService.order();
        storageService.storage();
        return Boolean.TRUE;
    }

    // http://localhost:4003/business/insertFail
    @GlobalTransactional(timeoutMills = 300000, name = "spring-cloud-demo-tx")
    @GetMapping("insertFail")
    public Boolean insertFail(){
        orderService.order();
        int x = 1 / 0;
        storageService.storage();
        return Boolean.TRUE;
    }

}
